home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Core / RefCtObj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  5.6 KB  |  177 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        RefCtObj.cpp
  3.  
  4.     Contains:    Implementation of ODRefCntObject.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <12>    10/24/95    jpa        1293441: Don't throw kODErrZeroRefCount,
  13.                                     only warn. (Also added more ref-count
  14.                                     debugging support.)
  15.         <11>      8/3/95    RR        #1257260: Collapse B classes. Remove
  16.                                     somInit methods. Don't call IsInitialized
  17.                                     or SubclassResponsibility
  18.         <10>     6/30/95    JP        Enabled assert in SomUninit
  19.          <9>     6/28/95    RR        1242642 BB Mostly ref counting. Added class
  20.                                     name to output
  21.          <8>     6/22/95    jpa        Added extra warnings for releasing objects
  22.                                     with zero ref-counts and for deleting
  23.                                     objects with nonzero ref-counts. [1242642]
  24.          <7>     5/26/95    RR        #1251403: Multithreading naming support
  25.          <6>     5/25/95    jpa        Added debugging tools. [1253335]
  26.          <5>     4/26/95    CG        1211082 BB: 5$ Bugs need to be evaluated
  27.                                     and removed from Core
  28.          <4>     3/31/95    VL        1233725: Changed THROW to SetSOMException
  29.                                     in Release.
  30.          <3>     1/20/95    JBS        1195017, 1205669: part API changes
  31.          <2>     9/22/94    JBS        1188221: initialize fRefCount to 0
  32.          <1>     6/23/94    CG        first checked in
  33.         <11>      2/8/94    TÇ        Throw -> THROW & some code clean up
  34.         <10>     1/11/94    TÇ        Init... changes
  35.          <9>    12/15/93    TÇ        InitObject changes
  36.          <8>     12/3/93    TÇ        Stop including ODError.h, it is included
  37.                                     as ErrorDef.h inside Except.h
  38.          <7>    11/24/93    VL        Commented out changes in <6>.
  39.          <6>    11/11/93    TÇ        In destructor:    if (fRefCount>0) do warning
  40.                                     assert.  Actual Throw code in comment next
  41.                                     to the WASSERTM.
  42.          <5>     8/13/93    VL        Check for fRefCount == 0 in Release.
  43.          <4>      8/9/93    PH        Segments
  44.          <3>      8/6/93    VL        Implemented GetRefCount.
  45.          <2>     6/30/93    VL        RefCount should be 1 when an object is
  46.                                     created.
  47.          <1>     6/16/93    VL        first checked in
  48.  
  49.     To Do:
  50.     In Progress:
  51.         
  52. */
  53.  
  54. #define ODRefCntObject_Class_Source
  55. #define VARIABLE_MACROS
  56. #include <RefCtObj.xih>
  57.  
  58. #ifndef _EXCEPT_
  59. #include "Except.h"
  60. #endif    
  61.  
  62. #pragma segment ODRefCntObject
  63.  
  64.  
  65. #if ODDebug
  66.     /*    Debugging feature: If you set gTrackObj (presumably using a debugger) to point to
  67.         an object you're interested in, info will be written via somPrintf whenever that
  68.         object's reference count changes. This includes the current reference count and
  69.         a stack crawl.
  70.         If you enter a class name in gTrackClass, the ref-counts of all objects of that
  71.         class will be tracked & logged in the same way. */
  72.     
  73.     #include <Crawl.h>
  74.     
  75.     static ODBoolean gTrackAll = kODFalse;            // Set to true to see creation/deletion of all
  76.     static ODRefCntObject *gTrackObj = kODNULL;        // Edit at runtime to track obj
  77.     char gTrackClass[64] = "";                        // Edit at runtime to track all objs of a class
  78.     
  79.     static void DumpStack( long start )
  80.     {
  81.         StackCrawl *s = StackCrawl::New(start,-5);
  82.         for( long i=0; i<s->CountFrames(); i++ ) {
  83.             char name[256];
  84.             s->GetFrameName(i,name);
  85.             somPrintf("        %s\n",name);
  86.         }
  87.         delete s;
  88.     }
  89.     
  90.     static void LogRefCount( ODRefCntObject *obj, ODBoolean increment, ODULong refCount )
  91.     {
  92.         somPrintf("%s %s %p: Now %d\r",
  93.                         increment ?"+++ IncrementRefCount of" :"--- Release",
  94.                         obj->somGetClassName(),
  95.                         obj,
  96.                         refCount );
  97.         DumpStack(3);
  98.     }
  99. #endif
  100.  
  101.  
  102. SOM_Scope void  SOMLINK ODRefCntObjectInitRefCntObject(ODRefCntObject *somSelf, Environment *ev)
  103. {
  104.     ODRefCntObjectData *somThis = ODRefCntObjectGetData(somSelf);
  105.     ODRefCntObjectMethodDebug("ODRefCntObject","InitRefCntObject");
  106.  
  107.     /* Moved from somInit. SOM itself sets fields to zero
  108.     _fRefCount = 0;
  109.     */
  110.     somSelf->InitObject(ev);
  111.     
  112.     _fRefCount = 1;
  113.  
  114. #if ODDebug
  115.     if( gTrackAll || somSelf == gTrackObj || (gTrackClass[0] && strcmp(somSelf->somGetClassName(),gTrackClass)==0) )
  116.         LogRefCount(somSelf,kODTrue,_fRefCount);
  117. #endif
  118. }
  119.  
  120. SOM_Scope void  SOMLINK ODRefCntObjectAcquire(ODRefCntObject *somSelf, Environment *ev)
  121. {
  122.     ODRefCntObjectData *somThis = ODRefCntObjectGetData(somSelf);
  123.     ODRefCntObjectMethodDebug("ODRefCntObject","Acquire");
  124.  
  125.     _fRefCount++;
  126.  
  127. #if ODDebug
  128.     if( somSelf == gTrackObj || (gTrackClass[0] && strcmp(somSelf->somGetClassName(),gTrackClass)==0) )
  129.         LogRefCount(somSelf,kODTrue,_fRefCount);
  130. #endif
  131. }
  132.  
  133. SOM_Scope void  SOMLINK ODRefCntObjectRelease(ODRefCntObject *somSelf, Environment *ev)
  134. {
  135.     ODRefCntObjectData *somThis = ODRefCntObjectGetData(somSelf);
  136.     ODRefCntObjectMethodDebug("ODRefCntObject","Release");
  137.  
  138.     if (_fRefCount == 0) {
  139.         WARN("%s %p released one too many times", somSelf->somGetClassName(),somSelf);
  140. //        ODSetSOMException(ev, kODErrZeroRefCount);
  141.     } else
  142.         --_fRefCount;
  143.  
  144. #if ODDebug
  145.     if( somSelf == gTrackObj || (gTrackClass[0] && strcmp(somSelf->somGetClassName(),gTrackClass)==0) )
  146.         LogRefCount(somSelf,kODFalse,_fRefCount);
  147. #endif
  148. }
  149.  
  150. SOM_Scope ODULong  SOMLINK ODRefCntObjectGetRefCount(ODRefCntObject *somSelf, Environment *ev)
  151. {
  152.     ODRefCntObjectData *somThis = ODRefCntObjectGetData(somSelf);
  153.     ODRefCntObjectMethodDebug("ODRefCntObject","GetRefCount");
  154.  
  155.     return _fRefCount;
  156. }
  157.  
  158. SOM_Scope void  SOMLINK ODRefCntObjectsomUninit(ODRefCntObject *somSelf)
  159. {
  160.     ODRefCntObjectData *somThis = ODRefCntObjectGetData(somSelf);
  161.     ODRefCntObjectMethodDebug("ODRefCntObject","somUninit");
  162.  
  163. #if ODDebug
  164.     if( _fRefCount!=0 || gTrackAll
  165.                       || somSelf == gTrackObj
  166.                       || (gTrackClass[0] && strcmp(somSelf->somGetClassName(),gTrackClass)==0) ) {
  167.         somPrintf("XXX Deleted %s %p (with ref-count at %d)\r",
  168.                         somSelf->somGetClassName(), somSelf, _fRefCount);
  169.         DumpStack(5);
  170.         if( somSelf==gTrackObj )
  171.             gTrackObj = 0;
  172.         if( _fRefCount != 0 )
  173.             WARN("%s at %p deleted with refcount == %d", somSelf->somGetClassName(), somSelf, _fRefCount);
  174.     }
  175. #endif
  176. }
  177.